package cn.acewill.yxd.yunxundian.componet;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ProgressBar;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.utils.DensityUtil;

/**
 * Created by xxx on 2018/1/22.
 *
 */

public class MajorProgress extends ProgressBar{

    private String name = "",itemCount = "",progressValue = "",
            editValue = "",scoreValue = "";
    private Paint titlePaint;
    private Paint itemPaint;
    private Context mContext;

    public MajorProgress(Context context) {
        super(context);
        mContext = context;
        initPaint();
    }

    public MajorProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initPaint();
        getAttrs(context,attrs);
    }

    public MajorProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initPaint();
        getAttrs(context,attrs);
    }

    private void getAttrs(Context context,AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MajorProgress);
        name = ta.getString(R.styleable.MajorProgress_majorName);
        itemCount = ta.getString(R.styleable.MajorProgress_itemCount);
        ta.recycle();
    }
    private void initPaint(){
        titlePaint = new Paint();
        titlePaint.setColor(getResources().getColor(R.color.titleColor));
        titlePaint.setTextSize(DensityUtil.dip2px(mContext,14));
        titlePaint.setAntiAlias(true);
        titlePaint.setStrokeWidth(5);
        itemPaint = new Paint();
        itemPaint.setTextSize(DensityUtil.dip2px(mContext,10));
        itemPaint.setAntiAlias(true);
        itemPaint.setColor(getResources().getColor(R.color.left_itemColor));
    }

    @Override
    public synchronized void setProgress(int progress) {
   //     setText(progress);
        super.setProgress(progress);
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //初始坐标设置
        Rect rect = new Rect();
        titlePaint.getTextBounds(name,0,name.length(),rect);
        float mRight = DensityUtil.dip2px(mContext,6);
        float xName = DensityUtil.dip2px(mContext,15);
        float yName = getHeight()/2-DensityUtil.dip2px(mContext,8)-rect.centerY();
        Rect rect1 = new Rect();
        itemPaint.getTextBounds(itemCount,0,itemCount.length(),rect1);
        float yItemCount = getHeight() - DensityUtil.dip2px(mContext,8);

       // String text = scoreValue+"/"+progressValue;
        String text = scoreValue;
        itemPaint.getTextBounds(text,0,text.length(),rect1);
       // float x = ((getWidth())/2)-rect1.centerX()/2+(((getWidth()/2)-rect1.centerX()/2)/2); //3/4
        float x = getWidth()-(rect1.width()+mRight); //贴近侧边
        float y = getHeight()/2 - rect1.centerY();
        Log.e("====x====",x+"---"+y);

        float xEdit = xName+150;
        float yEdit = yItemCount;
        //绘制UI
        canvas.drawText(name,xName,yName,titlePaint);
        canvas.drawText(itemCount, xName,yItemCount,itemPaint);
        canvas.drawText(editValue,xEdit,yEdit,itemPaint);
        canvas.drawText(text,x,y,itemPaint);
        Log.e("getWidth",getWidth()+"=="+getHeight());

    }

    private void setText(double progress){
        double value = progress*100/this.getMax();
        progressValue = String .valueOf(value)+"%";
    }

    public void setMajorName(String str){
       name = str;
    }
    public void setItemCount(String value){
        itemCount = value;
    }
    public void setEditValue(String str){
        editValue = str;
    }
    public void setScoreValue(String score){
        this.scoreValue = score;
    }
}
